package com.collect.zambia.zhiScene.zhiCheckIn;

import android.content.Context;

import com.collect.zambia.zhiBase.BasePresenter;
import com.collect.zambia.zhiBase.BaseSubscriber;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRCheckInOutEntity;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRSysConfigEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISCheckInOutEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISendEntity;
import com.collect.zambia.zhiHelper.ZHIMainProcessHelper;
import com.collect.zambia.zhiNetApi.ZHIHttpApiProvider;
import com.collect.zambia.zhiNetApi.ZHIHttpResponse;
import com.collect.zambia.zhiUtils.RxSchedulers;

import java.util.ArrayList;

import javax.inject.Inject;

public class ZHICheckInPresenter extends BasePresenter<ZHICheckInContract.View> implements ZHICheckInContract.Presenter {

    @Inject
    public ZHICheckInPresenter() {
    }

    @Override
    public void getCheckState(Context context, ZHISendEntity zhiSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRCheckInOutEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRCheckInOutEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRCheckInOutEntity zhirCheckInOutEntity = (ZHIRCheckInOutEntity) object;
                            ZHIMainProcessHelper.getInstance().setZHIRCheckInOutEntity(zhirCheckInOutEntity);
                            mView.onGetCheckState(zhirCheckInOutEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo("getCheckState");
                    }
                };
        ZHIHttpApiProvider.getApi().checkInOutState(zhiSendEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getCheckStateWhenCheckedCompleted(Context context, ZHISendEntity zhiSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRCheckInOutEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRCheckInOutEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRCheckInOutEntity zhirCheckInOutEntity = (ZHIRCheckInOutEntity) object;
                            ZHIMainProcessHelper.getInstance().setZHIRCheckInOutEntity(zhirCheckInOutEntity);
                            mView.onGetCheckStateWhenCheckedCompleted(zhirCheckInOutEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo("getCheckStateWhenCheckedCompleted");
                    }
                };
        ZHIHttpApiProvider.getApi().checkInOutState(zhiSendEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void checkInOut(Context context, ZHISCheckInOutEntity zhisCheckInOutEntity) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onCheckInOut();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessageL(messge);
                        mView.onGetErrorInfo("checkInOut");
                    }
                };
        ZHIHttpApiProvider.getApi().checkInOut(zhisCheckInOutEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getCheckHistory(Context context, ZHISendEntity zhiSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRCheckInOutEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRCheckInOutEntity>>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRCheckInOutEntity> zhirCheckInOutEntityArrayList = (ArrayList<ZHIRCheckInOutEntity>)object;
                            mView.onGetCheckHistory(zhirCheckInOutEntityArrayList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo("getCheckHistory");
                    }
                };
        ZHIHttpApiProvider.getApi().checkInOutHistory(zhiSendEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getAppConfig(Context context, ZHISendEntity zhiSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRSysConfigEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRSysConfigEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRSysConfigEntity zhirSysConfigEntity = (ZHIRSysConfigEntity) object;
                            mView.onGetAppConfig(zhirSysConfigEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo("getAppConfig");
                    }
                };
        ZHIHttpApiProvider.getApi().sysConfig(zhiSendEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }
}
